﻿Imports System.Windows.Forms
Imports System.Xml

Public Class mdiMain

    Private s As String = ""

    Private Function NewInfoBox(ByVal argTitle)
        Dim NewMDIChild As New frmBox()
        NewMDIChild.Text = argTitle
        'Set the Parent Form of the Child window.
        NewMDIChild.MdiParent = Me
        'Display the new form.
        NewMDIChild.Show()
        NewInfoBox = NewMDIChild
    End Function

    Public Sub AddNewChild(ByVal argCaption, ByVal argBody)
        Dim tmpCaption As String = Trim(argCaption)
        tmpCaption = Replace(tmpCaption, " ...", "")
        Dim box As frmBox = NewInfoBox(Strings.Replace(tmpCaption, "&", ""))
        Dim xOut As String = argBody
        If Strings.Trim(xOut) = "" Then xOut = "(chưa có nội dung)"
        box.Append(xOut)
        s = ""
    End Sub

    'Michael Khuyen
    'Tạo thực đơn từ tệp xml
    Public Function CreateMenuXML(ByRef menu As ToolStripMenuItem, ByVal PathElement As String, ByVal file As String) As ToolStripMenuItem
        Dim objXML As Xml.XmlDocument = New Xml.XmlDocument()
        ' objXML = New Xml.XmlDocument()
        objXML.Load(file)
        Dim objNode As Xml.XmlNode
        Dim objNode1 As Xml.XmlNode
        objNode1 = objXML.DocumentElement.SelectSingleNode("/" + PathElement)
        For Each objNode In objNode1.ChildNodes ' objXML.FirstChild.ChildNodes
            Dim strmenu As New ToolStripMenuItem()
            strmenu.Name = objNode.Attributes.GetNamedItem("type").Value.Replace(" ", "")
            strmenu.Text = objNode.Attributes.GetNamedItem("type").Value
            strmenu.Tag = objNode.Attributes.GetNamedItem("type").Value.Replace(" ", "")
            AddHandler strmenu.Click, AddressOf MenuItemOnClick
            menu.DropDownItems.Add(strmenu)
            For Each n As Xml.XmlNode In objNode.ChildNodes
                Dim sMenu1 As New ToolStripMenuItem()
                sMenu1.Text = n.Name
                sMenu1.Name = n.Name.Replace(" ", "")
                sMenu1.Tag = n.Name.Replace(" ", "")
                AddHandler sMenu1.Click, AddressOf MenuItemOnClick
                strmenu.DropDownItems.Add(sMenu1)
            Next n
        Next
        Return menu
    End Function
    'Michael khuyen
    'tạo sự kiện click cho các thực đơn
    Private Sub MenuItemOnClick(ByVal sender As Object, ByVal e As System.EventArgs)
        'MessageBox.Show("Test Clicked")
        Dim menu As ToolStripMenuItem
        menu = sender
        ' MsgBox(menu.Text)

    End Sub
    Private Sub mdiMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With My.Application.Info
            Me.Text = .ProductName
            Dim s As String = .ProductName & " version " & .Version.ToString
            Me.ToolStripStatusLabel1.Text = s
        End With
        'michael khuyen
        'goi ham tạo thực đơn
        CreateMenuXML(SubjectsToolStripMenuItem, "RootSubject", Application.StartupPath.Replace("bin", "Xml").Replace("Debug", "XmlConfigSubjects.xml"))
    End Sub



    Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs) Handles NewToolStripMenuItem.Click, NewToolStripButton.Click, NewWindowToolStripMenuItem.Click
        Dim frmBrowserTree As New frmBrowserTree()
        frmBrowserTree.MdiParent = Me
        m_ChildFormNumber += 1
        frmBrowserTree.Text = "BrowserTree " & m_ChildFormNumber
        frmBrowserTree.Show()
    End Sub

    Private Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs) Handles OpenToolStripMenuItem.Click, OpenToolStripButton.Click
        Dim OpenFileDialog As New OpenFileDialog
        OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        OpenFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
            Dim FileName As String = OpenFileDialog.FileName
            ' TODO: Add code here to open the file.
        End If
    End Sub

    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveAsToolStripMenuItem.Click
        Dim SaveFileDialog As New SaveFileDialog
        SaveFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        SaveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"

        If (SaveFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
            Dim FileName As String = SaveFileDialog.FileName
            ' TODO: Add code here to save the current contents of the form to a file.
        End If
    End Sub

    Private Sub ExitToolsStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub CutToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CutToolStripMenuItem.Click
        ' Use My.Computer.Clipboard to insert the selected text or images into the clipboard
    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CopyToolStripMenuItem.Click
        ' Use My.Computer.Clipboard to insert the selected text or images into the clipboard
    End Sub

    Private Sub PasteToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PasteToolStripMenuItem.Click
        'Use My.Computer.Clipboard.GetText() or My.Computer.Clipboard.GetData to retrieve information from the clipboard.
    End Sub

    Private Sub ToolBarToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ToolBarToolStripMenuItem.Click
        Me.ToolStrip.Visible = Me.ToolBarToolStripMenuItem.Checked
    End Sub

    Private Sub StatusBarToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles StatusBarToolStripMenuItem.Click
        Me.StatusStrip.Visible = Me.StatusBarToolStripMenuItem.Checked
    End Sub

    Private Sub CascadeToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CascadeToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.Cascade)
    End Sub

    Private Sub TileVerticalToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles TileVerticalToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.TileVertical)
    End Sub

    Private Sub TileHorizontalToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles TileHorizontalToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.TileHorizontal)
    End Sub

    Private Sub ArrangeIconsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ArrangeIconsToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.ArrangeIcons)
    End Sub

    Private Sub CloseAllToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CloseAllToolStripMenuItem.Click
        ' Close all child forms of the parent.
        For Each ChildForm As Form In Me.MdiChildren
            ChildForm.Close()
        Next
    End Sub

    Private m_ChildFormNumber As Integer


    Private Sub LoggingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoggingToolStripMenuItem.Click
        LoggingWithinClass()
    End Sub
    Public Sub LoggingWithinClass()
        ClearTraceLog(False)
        StartLogging()
        LogEntry(Me.GetType.Name, System.Reflection.MethodBase.GetCurrentMethod().Name)
        LogMsg("I want to <b><font color=green>log this message</font></b> in HTML.")
        LogExit(Me.GetType.Name, System.Reflection.MethodBase.GetCurrentMethod().Name)
        StopLogging()
        ViewTraceLog()
    End Sub
    Public Function ToggleTreeView()
        Dim frmTree As New frmTree()
        If TreeToolStripMenuItem.Checked Then
            With frmTree.SplitContainer1
                .Panel1MinSize = 0
                .Panel2MinSize = 0
                .SplitterDistance = 0
            End With
            TreeToolStripMenuItem.Checked = False
        Else
            With frmTree.SplitContainer1
                .Panel1MinSize = 25
                .Panel2MinSize = 25
                .SplitterDistance = .Width / 3
            End With
            TreeToolStripMenuItem.Checked = True
        End If
        ToggleTreeView = TreeToolStripMenuItem.Checked
    End Function
    Private Sub TreeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TreeToolStripMenuItem.Click
        Dim frmTree As New frmTree()
        frmTree.MdiParent = Me
        frmTree.Show()
        ToggleTreeView()
    End Sub


    Private Sub DataBaseDLLToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataBaseDLLToolStripMenuItem.Click

    End Sub

    Private Sub TypeConvertToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TypeConvertToolStripMenuItem.Click

    End Sub

    Private Sub MonitorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MonitorToolStripMenuItem.Click

    End Sub

    Private Sub ProccessBarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProccessBarToolStripMenuItem.Click

    End Sub

    Private Sub EmailToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmailToolStripMenuItem.Click

    End Sub

    Private Sub XMLToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XMLToolStripMenuItem.Click

    End Sub

    Private Sub SecurityToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SecurityToolStripMenuItem.Click

    End Sub

    Private Sub FileSearchToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileSearchToolStripMenuItem.Click
        Dim fr As New frmSearch

        fr.MdiParent = Me
        fr.Show()
    End Sub

    Private Sub MDIToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MDIToolStripMenuItem.Click

    End Sub

    Private Sub SubjectsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubjectsToolStripMenuItem.Click
        Dim fr As New frmSubjects

        fr.MdiParent = Me
        fr.Show()
    End Sub

    Private Sub UserStoryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UserStoryToolStripMenuItem.Click
        Dim fr As New frmStory

        fr.MdiParent = Me
        fr.Show()
    End Sub

    Private Sub OrganizationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrganizationToolStripMenuItem.Click
        Dim _frmNewOrganization As New frmNewOrganization()
        _frmNewOrganization.MdiParent = Me
        m_ChildFormNumber += 1
        _frmNewOrganization.Text = "New Organization " & m_ChildFormNumber
        _frmNewOrganization.Show()
    End Sub

    Private Sub RoomToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RoomToolStripMenuItem.Click
        Dim _frmNewMeetingRoom As New frmNewMeetingRoom()
        _frmNewMeetingRoom.MdiParent = Me
        m_ChildFormNumber += 1
        _frmNewMeetingRoom.Text = "New Organization " & m_ChildFormNumber
        _frmNewMeetingRoom.Show()
    End Sub
    Dim m_ChildFormNumberStoryTDN As Int32
    Private Sub StoryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StoryToolStripMenuItem.Click
        m_ChildFormNumberStoryTDN += 1
        Dim stry As New frmStoryTDN()
        stry.MdiParent = Me
        stry.Text = "StoryTDN " & m_ChildFormNumberStoryTDN
        stry.statusnew = True
        stry.Show()
    End Sub
    Dim m_ChildFormNumberGP As Int32
    Private Sub MeetingRoomToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MeetingRoomToolStripMenuItem1.Click
        m_ChildFormNumberGP += 1
        Dim MeetingRoom As New frmMeetingRoom()
        MeetingRoom.MdiParent = Me
        MeetingRoom.Text = "MeetingRoom " & m_ChildFormNumberGP
        MeetingRoom.Show()
        'SetControl()
        MeetingRoom.MyRichTextBox_Name.Focus()
    End Sub
    Dim m_ChildFormNumberOrganization As Int32
    Private Sub OrganizationToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrganizationToolStripMenuItem1.Click
        m_ChildFormNumberOrganization += 1
        Dim Organization As New frmOrganization()
        Organization.MdiParent = Me
        Organization.Text = "Organization " & m_ChildFormNumberOrganization
        Organization.statusnew = True
        Organization.Show()
    End Sub
    Dim m_ChildFormNumberNS As Int32
    Private Sub PesonalToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PesonalToolStripMenuItem.Click
        m_ChildFormNumberNS += 1
        Dim Personal As New frmPersonal()
        Personal.MdiParent = Me
        Personal.Text = "Nhân sự " & m_ChildFormNumberNS
        Personal.Show()
        'SetControl()
        Personal.RichTextBox_VietNameseName.Focus()
    End Sub
    Dim m_ChildFormNumberInterested As Int32
    Private Sub InterestedToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InterestedToolStripMenuItem.Click
        m_ChildFormNumberInterested += 1
        Dim Interes As New frmInterest()
        Interes.MdiParent = Me
        Interes.Text = "Interes " & m_ChildFormNumberInterested
        Interes.statusnew = True
        Interes.Show()
    End Sub

    Private Sub CustomerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomerToolStripMenuItem.Click

    End Sub

    Private Sub MeetingRoomToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ToolStripMenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem4.Click
        Dim OpenFileDialog As New OpenFileDialog

        ' OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        OpenFileDialog.Filter = "Text Files (*.gp)|*.gp|Text Files (*.kh)|*.kh|Text Files (*.ns)|*.ns|Text Files (*.qh)|*.qh|Text Files (*.qt)|*.qt|Text Files (*.tc)|*.tc|Text Files (*.th)|*.th|All Files (*.*)|*.*"

        If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then

            Dim FileName As String = OpenFileDialog.FileName
            Dim oFiles As New IO.FileInfo(FileName) ' Include Path and filename
            Select Case oFiles.Extension
                Case ".tc"
                    'Dim tochuc As New frmToChuc()
                    'ReadXML(tochuc, FileName, oFiles.FullName)
                Case ".qh"
                    'Dim quanhe As New frmQuanHe()
                    'ReadXML(quanhe, FileName, oFiles.FullName)
                Case ".qt"
                    'Dim quanTam As New frmQuanTam()
                    'ReadXML(quanTam, FileName, oFiles.FullName)
                Case ".th"
                    'Dim truyenHe As New frmTruyenHe()
                    'ReadXML(truyenHe, FileName, oFiles.FullName)
                Case ".kh"
                    'Dim khachhang As New frmKhachHang()
                    'ReadXML(khachhang, FileName, oFiles.FullName)
                Case ".ns"
                    'Dim nhansu As New frmNhanSu()
                    'ReadXML(nhansu, FileName, oFiles.FullName)
                Case ".gp"
                    Dim MeetingRoom As New frmMeetingRoom()
                    ReadXML(MeetingRoom, FileName, oFiles.FullName)

                Case Else
                    ' does some processing... 
                    Exit Select
            End Select

        End If
    End Sub

    Private Sub ReadXML(ByVal frmName As Object, ByVal FileName As String, ByVal FullName As String)
        Dim ExistFile As Boolean = True
        Dim document As New XmlDocument
        For Each ChildForm As Form In Me.MdiChildren
            If (ChildForm.Text = FullName) Then
                ExistFile = False
                ChildForm.Activate()
                Exit Sub
            End If
        Next
        frmName.MdiParent = Me
        frmName.Text = FullName
        frmName.Show()
        For Each ctl In frmName.Controls
            If TypeOf ctl Is DataGridView Then
                LoadXMLMeetingRoomForDatable(FileName, ctl)
            End If
        Next
        'frmName.bStatus = False
    End Sub
    'Load data from file to Datatable
    Public Sub LoadXMLMeetingRoomForDatable(ByVal FilePath As String, ByVal DataGridView_MeetingRoom As Object)
        Try
            'Create table meetingroom
            Dim dataTable As DataTable = New DataTable("MeetingRoom")
            Dim Name As DataColumn = New DataColumn("Name", Type.GetType("System.String"))
            Dim Description As DataColumn = New DataColumn("Description", Type.GetType("System.String"))
            Dim Organization As DataColumn = New DataColumn("Organization", Type.GetType("System.String"))
            Dim Rules As DataColumn = New DataColumn("Rules", Type.GetType("System.String"))
            Dim Status As DataColumn = New DataColumn("Status", Type.GetType("System.String"))
            Dim Severity As DataColumn = New DataColumn("Severity", Type.GetType("System.String"))
            Dim Member As DataColumn = New DataColumn("Member", Type.GetType("System.String"))
            dataTable.Columns.Add(Name)
            dataTable.Columns.Add(Description)
            dataTable.Columns.Add(Organization)
            dataTable.Columns.Add(Rules)
            dataTable.Columns.Add(Status)
            dataTable.Columns.Add(Severity)
            dataTable.Columns.Add(Member)
            Dim xmldoc As New XmlDocument

            xmldoc.Load(FilePath)
            Dim nodeList As XmlNodeList = xmldoc.GetElementsByTagName("MeetingRoom")
            For Each node As XmlNode In nodeList
                Dim i As Integer = 0
                Dim dataRow As DataRow = dataTable.NewRow()
                For Each nodechild As XmlNode In node.ChildNodes
                    dataRow.Item(i) = nodechild.InnerText
                    i += 1
                Next
                dataTable.Rows.Add(dataRow)
            Next
            DataGridView_MeetingRoom.DataSource = dataTable
        Catch ex As Exception
            MessageBox.Show("Have a error in open file process.Please try again")
        End Try
    End Sub

    Private Sub InterestedToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InterestedToolStripMenuItem1.Click
        Dim OpenFileDialog As New OpenFileDialog
        ' OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        OpenFileDialog.Filter = "Text Files  (*.qt)|*.qt|All Files (*.*)|*.*"
        If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
            m_ChildFormNumberInterested += 1
            Dim Interes As New frmInterest()
            Interes.MdiParent = Me
            Interes.Text = "Interes " & m_ChildFormNumberInterested
            Interes.path = OpenFileDialog.FileName
            Interes.statusnew = False
            Interes.Show()
        End If

        
    End Sub

    Private Sub OrganizationToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrganizationToolStripMenuItem2.Click
        Dim OpenFileDialog As New OpenFileDialog
        ' OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        OpenFileDialog.Filter = "Text Files  (*.org)|*.org|All Files (*.*)|*.*"
        If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
            m_ChildFormNumberOrganization += 1
            Dim Organization As New frmOrganization()
            Organization.MdiParent = Me
            Organization.Text = "Organization" & m_ChildFormNumberOrganization
            Organization.path = OpenFileDialog.FileName
            Organization.statusnew = False
            Organization.Show()
        End If

    End Sub

    Private Sub StoryToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StoryToolStripMenuItem2.Click
        Dim OpenFileDialog As New OpenFileDialog
        ' OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        OpenFileDialog.Filter = "Text Files  (*.th)|*.th|All Files (*.*)|*.*"
        If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then

            Dim story As New frmStory()
            story.MdiParent = Me
            story.mode = "Old"
            story.filename = OpenFileDialog.FileName        
            story.Show()
        End If
    End Sub

    Private Sub StoryToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StoryToolStripMenuItem1.Click

        Dim OpenFileDialog As New OpenFileDialog
        ' OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        OpenFileDialog.Filter = "Text Files  (*.th)|*.th|All Files (*.*)|*.*"
        If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
            m_ChildFormNumberStoryTDN += 1
            Dim stry As New frmStoryTDN()
            stry.MdiParent = Me
            stry.Text = "StoryTDN " & m_ChildFormNumberStoryTDN
            stry.path = OpenFileDialog.FileName
            stry.statusnew = False
            stry.Show()
        End If
    End Sub
End Class
